package com.qf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qf.pojo.Customer;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 千锋健哥
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/one/{id}")
    //开启熔断功能
    @HystrixCommand(fallbackMethod = "findOneFallback")
    public Customer findOne(@PathVariable(value = "id") Integer id) {
        //模拟异常
        int i = 1/0;
        return new Customer(1, "青龙", 18, "我是一条小青龙");
    }

    /**
     * 降级方法, 如果上面的方法出错, 那么会自动调用下面这个方法执行, 返回结果
     * @param id
     * @return
     */
    public Customer findOneFallback(Integer id) {
        return new Customer(0, "search服务的锅", 0, "自己的锅我自己背!");
    }

    @GetMapping("/all")
    public List<Customer> findAll() {
        List<Customer> list = new ArrayList<>();
        list.add(new Customer(1, "青龙", 18, "我是一条小青龙"));
        list.add(new Customer(2, "奥龙", 19, "我是一条小奥龙"));
        list.add(new Customer(3, "波龙", 20, "我是一条小波龙"));

        return list;
    }

    @PostMapping("/add")
    public String add(@RequestBody Customer customer) {
        System.out.println("==========" + customer);
        return "添加成功!";
    }

    @PutMapping("/update")
    public String update(@RequestBody Customer customer) {
        System.out.println("==========" + customer);
        return "修改成功!";
    }

    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable(value = "id") Integer id) {
        System.out.println("==========" + id);
        return "删除成功!";
    }
}
